package com.lw.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.lw.common.license.LicenseCheckInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sa-token拦截器
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //license认证
//        registry.addInterceptor(new LicenseCheckInterceptor()).excludePathPatterns("/licenseCreator/**");//排除
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> {
                    SaRouter.match("/**")
                            .check(r -> {
                                StpUtil.checkLogin();
                            }).notMatch("/tbUser/doLogin/"); //排除登录接口 或者注解@SaIgnore
                })).excludePathPatterns("/swagger-ui/**","/v3/api-docs/**"); //排除swagger页面和api接口
    }

}
